package com.zoro.jianyue.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.zoro.jianyue.R;
import com.zoro.jianyue.bean.NavigationEntity;
import com.zoro.jianyue.libs.adapter.ListViewDataAdapter;
import com.zoro.jianyue.libs.adapter.ViewHolderBase;
import com.zoro.jianyue.libs.adapter.ViewHolderCreator;
import com.zoro.jianyue.ui.adapter.VPFragmentAdapter;
import com.zoro.jianyue.view.HomeView;
import com.zoro.jianyue.presenter.Presenter;
import com.zoro.jianyue.presenter.impl.HomePresenterImpl;
import com.zoro.jianyue.ui.base.BaseActivity;
import com.zoro.jianyue.ui.base.BaseLazyFragment;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import com.zoro.jianyue.widgets.XViewPager;

/**
 * Created by CM-ZR on 2016/1/19.
 */
public class HomeActivity extends BaseActivity implements HomeView {
    @Bind(R.id.home_container)
    XViewPager mViewPager;
    @Bind(R.id.home_navigation_list)
    ListView mNavListView;
    @Bind(R.id.home_drawer)
    DrawerLayout mDrawerLayout;

    private ListViewDataAdapter<NavigationEntity> mNavigatListAdapter = null;

    private int mCheckedListItemColorResIds[] = {
            R.color.navigation_checked_picture_text_color,
            R.color.navigation_checked_video_text_color,
            R.color.navigation_checked_music_text_color,
    };
    private int mCurrentMenuCheckedPos = 0;
    private ActionBarDrawerToggle mActionBarDrawerToggle = null;

    private Presenter mHomePresenter;
    private long double_click_exit;

    @Override
    protected void initViewsAndEvents() {
        mHomePresenter = new HomePresenterImpl(this, this);
        mHomePresenter.initialized();
    }

    @Override
    protected int setContentViewLayoutId() {
        return R.layout.activity_home;
    }

    @Override
    protected void getBudleExtras(Bundle extras) {

    }

    @Override
    protected View getLoadingTargetView() {
        return ButterKnife.findById(this, R.id.home_container);
    }

    @Override
    protected boolean isApplyKitKatTransLucency() {
        return true;
    }

    @Override
    public void initalizeViews(List<BaseLazyFragment> fragments, final List<NavigationEntity> navigationEntities) {
        mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.drawer_open, R.string.drawer_close) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                setTitle(R.string.app_name);
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                if (null != mNavigatListAdapter) {
                    setTitle(mNavigatListAdapter.getItem(mCurrentMenuCheckedPos).getName());
                }
            }
        };

        mActionBarDrawerToggle.setDrawerIndicatorEnabled(true);
        mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);

        if (null != fragments && !fragments.isEmpty()) {
            mViewPager.setEnableScroll(false);
            mViewPager.setOffscreenPageLimit(fragments.size());
            mViewPager.setAdapter(new VPFragmentAdapter(getSupportFragmentManager(), fragments));
        }

        mNavigatListAdapter = new ListViewDataAdapter<>(new ViewHolderCreator<NavigationEntity>() {
            @Override
            public ViewHolderBase<NavigationEntity> createViewHolder(int position) {
                return new ViewHolderBase<NavigationEntity>() {
                    ImageView imageView;
                    TextView textView;

                    @Override
                    public View createView(LayoutInflater layoutInflater) {
                        View convertView = layoutInflater.inflate(R.layout.list_item_navigation, null);
                        imageView = (ImageView) convertView.findViewById(R.id.list_item_navigation_icon);
                        textView = (TextView) convertView.findViewById(R.id.list_item_navigation_name);
                        return convertView;
                    }

                    @Override
                    public void showData(int position, NavigationEntity itemData) {
                        imageView.setImageResource(itemData.getIconResId());
                        textView.setText(itemData.getName());

                        if (mCurrentMenuCheckedPos == position) {
                            textView.setTextColor(mCheckedListItemColorResIds[position]);
                        } else {
                            textView.setTextColor(Color.BLACK);
                        }
                    }
                };
            }
        });

        mNavListView.setAdapter(mNavigatListAdapter);
        mNavigatListAdapter.getDataList().addAll(navigationEntities);
        mNavigatListAdapter.notifyDataSetChanged();
        setTitle(mNavigatListAdapter.getItem(mCurrentMenuCheckedPos).getName());

        mNavListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mCurrentMenuCheckedPos = position;
                mNavigatListAdapter.notifyDataSetChanged();
                mDrawerLayout.closeDrawer(Gravity.LEFT);
                mViewPager.setCurrentItem(mCurrentMenuCheckedPos, false);
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_MENU) {
            if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
                mDrawerLayout.closeDrawer(Gravity.LEFT);
            } else {
                mDrawerLayout.openDrawer(Gravity.LEFT);
            }
            return true;
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
                mDrawerLayout.closeDrawer(Gravity.LEFT);
            } else {
                if ((System.currentTimeMillis() - double_click_exit) > 2000) {
                    showToast(getString(R.string.double_click_exit));
                }
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
